import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)
//$store.dispatch('action中的方法名',数据) 调用
const actions = {
  cacheUserInfo(context,value){
    context.commit('cacheUserInfoWrapper',value)
  },
  changeRole(context,value){
    context.commit('changeRoleWrapper',value)
  }
}

//$store.commit('mutations中的方法名',数据) 调用
const mutations = {
  changeRoleWrapper(state,value){
    state.currentRole=value;
  },

  cacheUserInfoWrapper(state,value){
    state.userInfo=value;
    let roles=value.roles;
    if(roles && roles.length>0){
      state.roles=roles;
      state.currentRole=roles[0].departmentRoleId;
    }
    state.empId=value.empId;
    state.name=value.name;
  },
}

//$store.state.user_name 获取数据
const state = {
  userInfo:{},
  currentRole:'',
  roles:[],
  empId: "",
  name:"",
}

// $store.getters.userName   获取数据
const getters = {
   roles(state){
     return state.roles
   },
  currentRole(state){
     return state.currentRole;
  },
  empId(state){
     return state.empId;
  },
  name(state){
    return state.name;
  }
}

const store = new Vuex.Store({
  actions,
  mutations,
  state,
  getters
})
export { store }

